<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
         table {
            width: 800px;
            margin: 100px auto;
            text-align: center;
            border-collapse: collapse;
            font-size: 14px;
        }
        
        thead tr {
            height: 30px;
            background-color: skyblue;
        }
        
        tbody tr {
            height: 30px;
        }
        
        tbody td {
            border-bottom: 1px solid #d7d7d7;
            font-size: 12px;
            color: blue;
        }
        
        .bg {
            background-color: pink;
        }
    </style>
</head>
<body>
    <table>
        <thead>
            <tr>
                <th>
                    <input type="checkbox" id="j_checkBox">
                </th>
                <th>商品</th>
                <th>价钱</th>
            </tr>
        </thead>

        <tbody id="j_tb">
            <tr>
                <td> <input type="checkbox"></td>
                <td>ipone8</td>
                <td>8000</td>
            </tr>
            <tr>
                <td> <input type="checkbox"></td>
                <td>ipone8</td>
                <td>8000</td>
            </tr>
            <tr>
                <td> <input type="checkbox"></td>
                <td>ipone8</td>
                <td>8000</td>
            </tr>
            <tr>
                <td> <input type="checkbox"></td>
                <td>ipone8</td>
                <td>8000</td>
            </tr>
            <tr>
                <td> <input type="checkbox"></td>
                <td>ipone8</td>
                <td>8000</td>
            </tr>
        </tbody>
    </table>
    <script>
        var j_checkBox = document.getElementById('j_checkBox');
        var j_tb =document.getElementById('j_tb').getElementsByTagName('input');
        j_checkBox.onclick = function(){
            for (var i =0;i<j_tb.length;i++){
                j_tb[i].checked = this.checked;
            }
        }
        
        for(var i = 0;i<j_tb.length;i++){
            j_tb[i].onclick = function(){
                var flag = true;//flag 控制全选按钮是否选中
                //每次点击下面的复选框都要循环检查其他所有复选框是否被选中
                for(var i =0 ; i<j_tb.length;i++){
                    if(!j_tb[i].checked){
                        flag =false;
                        break;
                    }
                }
                j_checkBox.checked = flag;
            }
        }
    </script>
</body>
</html>